package function;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @describe 通过数据库筛选账单信息在此窗体显示
 * 
 */

import javax.swing.*;

import UserDatabase.BillDatabase;

public class BillFrame extends JFrame{
	
	private String [] tableHead;
	private String [][] user;
	private JTable table;
	private JLabel l_tip;
	private JButton b_buy;
	private BillDatabase udb = new BillDatabase();
	private String SQL,Username;
	
	public BillFrame(String SQL,String Username) {
		this.setSize(800,500);
		this.setTitle("账单（显示未缴费账单）");
		this.setLocation(500,150);
		this.SQL = SQL;
		this.Username = Username;
		init();
		this.setVisible(true);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			
			public void windowClosing(WindowEvent e) {
				new UserFrame(Username);
				setVisible(false);
			}
			
		});
		
	}
	
	public void init() {
		udb.setSQL(SQL);
		user = udb.getRecord();
		tableHead = udb.getColumnName();
		table = new JTable(user,tableHead);
		
		l_tip = new JLabel("未缴费账单",JLabel.CENTER);
		
		b_buy = new JButton("一键缴费");
		b_buy.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JPanel f = new JPanel();
				if(user.length != 0) {
					udb.update_buy(Username);
					setVisible(false);
				}else {
					JOptionPane.showMessageDialog(f,"您没有账单，无需缴费！","提示框",JOptionPane.WARNING_MESSAGE);	
				}
			}
			
		});
		
		
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1,1,5,5));
		p.add(l_tip);
		
		JScrollPane sp = new JScrollPane(table);
		
		JPanel p_bottom = new JPanel();
		p_bottom.setLayout(new GridLayout(1,1,8,8));
		p_bottom.add(b_buy);
		
		this.add(p,BorderLayout.NORTH);
		this.add(sp,BorderLayout.CENTER);
		this.add(p_bottom,BorderLayout.SOUTH);
	}

	public String[][] getUser() {
		return user;
	}
	
}
